#ifndef LEETCODE_CN_CPP_PROBLEMS_2_H
#define LEETCODE_CN_CPP_PROBLEMS_2_H

#include "utils/list.h"

namespace problem_2
{

class Solution
{
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
    {
        ListNode tmpHead(0);
        ListNode *pre = &tmpHead;
        int c = 0;
        while (c > 0 || l1 != nullptr || l2 != nullptr)
        {
            if (l1 != nullptr)
            {
                c += l1->val;
                l1 = l1->next;
            }
            if (l2 != nullptr)
            {
                c += l2->val;
                l2 = l2->next;
            }
            auto node = new ListNode(c % 10);
            c /= 10;
            pre->next = node;
            pre = node;
        }
        return tmpHead.next;
    }
};

} // namespace problem_2

#endif //LEETCODE_CN_CPP_PROBLEMS_2_H